<?php
	require_once('environment.php');
	$temp->addSubMenu("Videos", "videos.php");
	$temp->addSubMenu("Photos", "photos.php");
	if($acc->isLogged()){
		$temp->addSubMenu("Moderate Videos", "videos.php?admin=mod");
		$temp->addSubMenu("Moderate Photos", "photos.php?admin=mod");
		$temp->addSubMenu("Change media page", "media.php?admin=change");
	}
	
	$admin = fixArr($_GET, 'admin');
	$photos = new PhotoAlbum($acc, $db);
	
	function throwError($where, $what) {
		print change_site("photos.php?admin={$where}&error={$what}");
		die();
	}
	
	switch($admin) {
		default:
			$temp->setTab(3, 1);
			$temp->currentPage("Home >> Media >> Photos");
			$temp->generate();
			$photos->listImages();
			break;
		case("mod"):
			if(!$acc->isLogged()) die(change_site("account.php"));
			$temp->setTab(3, 3);
			$temp->currentPage("Home >> Media >> Photos >> Moderate Photos");
			$temp->generate();
			print "<span style=\"color: red;\">".(fixArr($_GET, "error") ? fixArr($_GET, "error")."<br />\n" : "")."</span>\n";
			print "<a href=\"photos.php?admin=add\">Add an Image</a><br />\n";
			$sql = $db->Execute("SELECT * FROM `monster`.`photos` ORDER BY `AutoInc` ASC");
			if($sql->RecordCount() > 0) {
				while($row = $sql->FetchRow()) {
					print "<br />\n";
					print "{$row['Name']}<br />\n<a href=\"photos.php?admin=delete&id={$row['AutoInc']}\">Delete</a> | <a href=\"photos.php?admin=edit&id={$row['AutoInc']}\">Edit</a><br />\n";
				}
			}
			break;
		case("add"):
			if(!$acc->isLogged()) die(change_site("account.php"));
			$temp->setTab(3, 3);
			$temp->currentPage("Home >> Media >> Photos >> Moderate Photos >> Add an Image");
			$temp->generate();
			if(!fixArr($_POST, "name")) {
				print "To get a link to the image, upload it on a site like <a href=\"http://imageshack.us\">ImageShack.us</a> and copy and paste the DIRECT LINK into the \"link\" box.<br />\n<br />\n";
				print "<span style=\"color: red;\">".(fixArr($_GET, "error") ? fixArr($_GET, "error")."<br />\n" : "")."</span>";
				print "<form method=\"POST\" action=\"\">\n";
				print "<label>Image name: <input type=\"text\" name=\"name\" /></label><br />\n";
				print "<label>Image link: <input type=\"text\" name=\"link\" /></label><br />\n";
				print "<input type=\"submit\" value=\"Add\" /> <input type=\"reset\" value=\"Reset\" />\n";
				print "</form>\n";
			}else{
				$name = fixArr($_POST, "name");
				$source = fixArr($_POST, "link");
				if($photos->addImage($name, $source)) {
					print "Your image was added successfully.";
				}else {
					throwError("add", "The image link you entered appears to be invalid. Please copy and paste the full direct link.");
				}
			}
			break;
		case("edit"):
			if(!$acc->isLogged()) die(change_site("account.php"));
			$temp->setTab(3, 3);
			$id = fixArr($_GET, "id");
			$row = $photos->getImageByID($id);
			if(!$row) throwError("mod", "An image with this ID does not exist.");
			$temp->currentPage("Home >> Media >> Photos >> Moderate Photos >> Edit an Image");
			$temp->generate();
			if(!fixArr($_POST, "name")) {
				print "To get a link to the image, upload it on a site like <a href=\"http://imageshack.us\">ImageShack.us</a> and copy and paste the DIRECT LINK into the \"link\" box.<br />\n<br />\n";
				print "<span style=\"color: red;\">".(fixArr($_GET, "error") ? fixArr($_GET, "error")."<br />\n" : "")."</span>";
				print "<form method=\"POST\" action=\"\">\n";
				print "<label>Image name: <input type=\"text\" name=\"name\" value=\"{$row['Name']}\" /></label><br />\n";
				print "<label>Image link: <input type=\"text\" name=\"link\" value=\"{$row['Img Src']}\" /></label><br />\n";
				print "<input type=\"submit\" value=\"Change\" /> <input type=\"reset\" value=\"Reset\" />\n";
				print "</form>\n";
			}else{
				$return = $photos->editImage($id, fixArr($_POST, "name"), fixArr($_POST, "link"));
				if(is_array($return)) {
					throwError("edit&id={$id}", $return[1]);
				}else {
					print "The image has been edited successfully!";
				}
			}
			break;
		case("delete"):
			if(!$acc->isLogged()) die(change_site("account.php"));
			$temp->setTab(3, 3);
			$id = fixArr($_GET, "id");
			$row = $photos->getImageByID($id);
			if(!$row) throwError("mod", "An image with this ID does not exist.");
			$temp->currentPage("Home >> Media >> Photos >> Moderate Photos >> Delete an Image");
			$temp->generate();
			if(fixArr($_GET, "conf") !== "true") {
				print "Are you sure you want to delete this image ({$row['Name']})? This decision can NOT be undone!<br />\n<br />\n";
				print "<a href=\"photos.php?admin=mod\">No, do not delete this image.</a><br />\n<br />\n";
				print "<a href=\"photos.php?admin=delete&id={$id}&conf=true\">Yes! Delete this image!</a><br />";
			}else {
				$photos->removeImage($id);
				print "Your image has been successfully removed.\n";
			}
			break;
	}
?>
